<?php

require_once("login/src/Model/UserInfo.php");
require_once("Database/Model/DatabaseConnection.php");

class UserInfoDAL
{
    /**
    *@var DatabaseConnection 
    **/
	private $databaseConnection;

	public function __construct()
	{
		$this->databaseConnection = new DatabaseConnection();
	}

    /**
    *@return array() userlist 
    **/
	public function getUserList()
	{
		$sql = "SELECT `UserID`, Email, Username, Password FROM UserInfo";

		return $this->UserList($sql);	
	}

    /**
    *@param string
    *@return array() userInfolist 
    **/
	private function UserList($sql)
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);
        //$mysqli = new mysqli("localhost", "root", "", "workoutdiary");
            
		$stmt = $mysqli->prepare($sql);
        
                if ($stmt == FALSE) 
                {
                	echo "An error has occured:: UserInfoDAL";
                }

                
                $result = $stmt->execute();
                if ($result == FALSE) 
                {
                	echo "An error has occured:: Userlist";
                }

                $result = $stmt->bind_result($userID, $email, $username, $password);
                if ($result == FALSE)
                {
                	echo "An error has occured:: Userlist";
                }

                $userinfoList = array();

	            while ($stmt->fetch()) 
                {
	                $userinfoList[] = UserInfo::CreateUserInfoFromDatabase($userID,
	                													   $email, 
	                													   $username, 
	                													   $password);
	            }

	            $mysqli->close();
                return $userinfoList;
	}

    /**
    *@param UserInfo
    **/
	public function saveNewUser(UserInfo $userinfo)
	{
								//localhost ist för 127.0.0.1
		//$mysqli = new mysqli("localhost", "root", "", "workoutdiary"); 
        $mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

		$sql = "INSERT INTO userinfo 
                        (
                        		UserID,
                                Email, 
                                Username, 
                                Password
                        ) 
                        VALUES(?, ?, ?, ?)";

                $statement = $mysqli->prepare($sql);
                if ($statement === FALSE) 
                {
                    //throw new \Exception("prepare of $sql failed " . $this->mysqli->error); kanske lägga i en errorMessage class, 
                }


                $userID = $userinfo->getUserID();
			    $email = $userinfo->getEmail();
                $username = $userinfo->getUsername(); 
                $password = $userinfo->getPassword();

                if ($statement->bind_param("isss", $userID,
                								   $email, 
                                                   $username, 
                                                   $password) === FALSE) 
                {
                }

                if ($statement->execute() === FALSE) 
                {
                        // throw new \Exception("execute of $sql failed " . $statement->error);
                }
		$mysqli->close();
	}

}

